package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServicelmpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
        //设置分页查询参数
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        //调用Mapper的方法进行分页查询
        Page<Category> page = categoryMapper.page(categoryPageQueryDTO);
        //返回结果
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void save(CategoryDTO categoryDTO) {
        //创建Category对象
        Category category = new Category();
        //对象属性拷贝
        BeanUtils.copyProperties(categoryDTO, category);
        //设置时间
        //category.setCreateTime(LocalDateTime.now());
        //category.setUpdateTime(LocalDateTime.now());
        //设置创建人
        ///category.setCreateUser(BaseContext.getCurrentId());
        //设置修改人
        //category.setUpdateUser(BaseContext.getCurrentId());
        //插入数据
        categoryMapper.insert(category);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        //创建一个Category对象
        Category category = Category.builder().status(status).id(id).build();
        //更新数据
        categoryMapper.update(category);


    }

    @Override
    public void update(CategoryDTO categoryDTO) {
        //创建Category对象
        Category category = new Category();
        //对象属性拷贝
        BeanUtils.copyProperties(categoryDTO, category);
        //设置时间
        //category.setUpdateTime(LocalDateTime.now());
        //设置修改人
        //category.setUpdateUser(BaseContext.getCurrentId());
        //更新数据
        categoryMapper.update(category);
    }

    @Override
    public void delete(Long id) {
        //调用Mapper的方法进行删除
        categoryMapper.delete(id);

    }

    @Override
    public List<Category> list(Integer type) {
        //调用Mapper的方法进行查询
        return categoryMapper.list(type);
    }
}
